<template>
  <div class="chatList">
    <div class="tabsBox">
      <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
        <el-tab-pane label="我加入的" name="0"></el-tab-pane>
        <el-tab-pane label="我管理的" name="1"></el-tab-pane>
        <el-tab-pane label="我创建的" name="2"></el-tab-pane>
      </el-tabs>
    </div>
    <div class="chat-view" ref="containerRef" @scroll="handleScroll">
      <div
        class="chat-item"
        v-for="(item, key) in chatGroupMap"
        :key="key"
        :class="key === changeUser ? 'active' : ''"
        @click="changeItem(item, key)"
      >
        <div class="chat-img">
          <canvas
            :ref="(el) => getDivDom(el, item)"
            :style="{ background: ColorMap[key % 4] }"
          ></canvas>
        </div>
        <div class="chat-name">
          <div class="content-title">
            <span class="pc-name">{{ item.name }}</span>
          </div>
          <div class="content-txt">
            {{ item.ownerNickname }}
          </div>
        </div>
        <div class="chat-time">
          <div>{{ formatDate(item.createTime) }}</div>
          <div> </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { formatDate } from '@/utils/formatTime'
const divDomList = ref(new Map())
const ColorMap = ref([
  'rgb(132, 202, 176)',
  'rgb(188, 158, 221)',
  'rgb(125, 179, 217)',
  'rgb(140, 163, 205)'
])

const changeUser = ref('')
const containerRef = ref(null)
const props = defineProps({
  chatGroupMap: Array,
  activeName: String
})
const { activeName } = toRefs(props)
watch(
  () => divDomList.value,
  (value) => {
    props.chatGroupMap?.forEach((item) => {
      const canvas = value.get(item.chatId)
      if (canvas) {
        let companyName = item.name.slice(0, 2).toUpperCase() || ''
        let positLeft = 50
        const ctx = canvas.getContext('2d')
        ctx.scale(1, 0.5)
        ctx.font = '100px sans-serif'
        ctx.fillStyle = '#fff'
        ctx.fillText(companyName, positLeft, 180)
      }
    })
  },
  { immediate: true, deep: true }
)
const getDivDom = (el, item) => {
  if (el) {
    divDomList.value.set(item.chatId, el)
  }
}

const handleClick = (val) => {
  emit('setActiveName', val.index)
}
const emit = defineEmits(['selectChat', 'loadScroll', 'setActiveName'])

// 无限滚动
const handleScroll = () => {
  const container = containerRef.value
  if (container.scrollTop + container.clientHeight >= container.scrollHeight) {
    emit('loadScroll')
  }
}

const changeItem = (item, key) => {
  changeUser.value = key
  emit('selectChat', item, key)
}
</script>

<style lang="scss" scoped>
canvas {
  width: 40px;
  height: 40px;
  border-radius: 4px;
}
.chatList {
  flex: 0.7;
  height: 100%;
  background-color: #fff;
  background: #f0f0f1;
  .chat-view {
    height: calc(100% - 54px);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    &::-webkit-scrollbar {
      width: 2px;
    }
    &::-webkit-scrollbar-track {
      background: #f1f1f1;
    }
    &::-webkit-scrollbar-thumb {
      background: #d8d8d8;
    }
    &::-webkit-scrollbar-thumb:hover {
      background: #d8d8d8;
    }
    &::-webkit-scrollbar-thumb:horizontal:start {
      border-top-left-radius: 4px;
      border-bottom-left-radius: 4px;
    }
    &::-webkit-scrollbar-thumb:horizontal:end {
      border-top-right-radius: 4px;
      border-bottom-right-radius: 4px;
    }
    .active {
      background: #d1def0;
      border-radius: 4px;
    }
    .chat-item {
      width: calc(100% - 30px);
      padding: 12px 10px;
      border-bottom: 1px solid #e0e1e2;
      display: flex;
      cursor: pointer;
      .chat-img {
        position: relative;
        .el-image {
          width: 40px;
          height: 40px;
          border-radius: 50%;
        }
      }
      .chat-name {
        margin-left: 12px;
        width: 150px;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        .content-title {
          display: flex;
          .pc-name {
            font-size: 16px;
            color: #000;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            margin-right: 5px;
          }
        }
        .content-txt {
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          font-size: 12px;
          color: #959291;
        }
      }
      .chat-time {
        color: #959291;
        font-size: 12px;
        display: flex;
        flex-direction: column;
        justify-content: flex-end;
        align-items: flex-start;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        div {
          width: 100%;
          height: 50%;
          display: flex;
          align-items: center;
          justify-content: flex-end;
          span {
            display: inline-block;
            width: 10px;
            height: 10px;
            background: red;
            border-radius: 50%;
          }
        }
      }
    }
  }
}
.tabsBox {
  padding: 0 10px;
}
</style>
